{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy= 1.0\n",
      "Inference Time -13.06447148323059\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import time\n",
    "\n",
    "conv1_w=np.load('LeNet-weights/Conv1_weight.np.npy')\n",
    "conv1_b=np.load('LeNet-weights/Conv1_bias.np.npy')\n",
    "conv2_w=np.load('LeNet-weights/Conv2_weight.np.npy')\n",
    "conv2_b=np.load('LeNet-weights/Conv2_bias.np.npy')\n",
    "f1_w=np.load('LeNet-weights/fc1_weight.np.npy')\n",
    "f2_w=np.load('LeNet-weights/fc2_weight.np.npy')\n",
    "f1_b=np.load('LeNet-weights/fc1_bias.np.npy')\n",
    "f2_b=np.load('LeNet-weights/fc2_bias.np.npy')\n",
    "data=np.load('Mnist/mnist_data.npy')\n",
    "target1=np.load('Mnist/mnist_label.npy')\n",
    "\n",
    "def test(testnumber):\n",
    "        \n",
    "        total = 0\n",
    "        correct = 0\n",
    "        data2 = data[0:testnumber]\n",
    "        target=target1[0:testnumber]\n",
    "        size = data2.shape\n",
    "        t1=time.time()\n",
    "        #CONV1 (10,5,5)\n",
    "        rs = np.zeros((size[0], 10, 24, 24))\n",
    "        for i0 in range(0, size[0]):\n",
    "            for i in range(0, 10):\n",
    "                for i1 in range(0, 24):\n",
    "                    for i2 in range(0, 24):\n",
    "                        tmp = data2[i0, 0, i1:i1 + 5, i2:i2 + 5]\n",
    "                        rs[i0][i][i1][i2] = np.sum(np.multiply(tmp, conv1_w[i])) + conv1_b[i]\n",
    "        #POOLING\n",
    "        rs2 = np.zeros((size[0], 10, 12, 12))\n",
    "        for i0 in range(0, size[0]):\n",
    "            for i in range(0, 10):\n",
    "                i1=0\n",
    "                while i1<24:\n",
    "                    i2=0\n",
    "                    while i2<24:\n",
    "                        tmp = rs[i0, i, i1:i1 + 2, i2:i2 + 2]\n",
    "                        tmp=np.max(tmp)\n",
    "        #RELU\n",
    "                        if tmp>0:\n",
    "                            rs2[i0][i][int(i1/2)][int(i2/2)] =tmp\n",
    "                        i2+=2\n",
    "                    i1+=2\n",
    "        #conv2 (20,5,5)\n",
    "        rs = np.zeros((size[0], 20, 8, 8))\n",
    "        for i0 in range(0, size[0]):\n",
    "            for i in range(0, 20):\n",
    "                for i1 in range(0, 8):\n",
    "                    for i2 in range(0, 8):\n",
    "                        tmp = rs2[i0,:, i1:i1 + 5, i2:i2 + 5]\n",
    "                        rs[i0][i][i1][i2] = np.sum(np.multiply(tmp, conv2_w[i])) + conv2_b[i]\n",
    "        #POOLING\n",
    "        rs2 = np.zeros((size[0], 20, 4, 4))\n",
    "        for i0 in range(0, size[0]):\n",
    "            for i in range(0, 20):\n",
    "                i1=0\n",
    "                while i1<8:\n",
    "                    i2=0\n",
    "                    while i2<8:\n",
    "                        tmp = rs[i0, i, i1:i1 + 2, i2:i2 + 2]\n",
    "                        tmp=np.max(tmp)\n",
    "        #Relu\n",
    "                        if tmp>0:\n",
    "                            rs2[i0][i][int(i1/2)][int(i2/2)] =tmp\n",
    "                        i2+=2\n",
    "                    i1+=2\n",
    "        #Flatteren\n",
    "        rs = np.zeros((size[0],320))\n",
    "        #FC1 320 --->50\n",
    "        for i0 in range(0, size[0]):\n",
    "            tmp=rs2[i0]\n",
    "            rs[i0]=tmp.flatten()\n",
    "        rs2 = np.zeros((size[0], 50))\n",
    "        for i0 in range(0,size[0]):\n",
    "            tmp=rs[i0]\n",
    "            tmp=np.add(np.dot(f1_w, tmp), f1_b)\n",
    "            for i in range(0,50):\n",
    "        #RELU\n",
    "                if tmp[i]>0:\n",
    "                    rs2[i0][i]=tmp[i]\n",
    "        #FC2 50 ---> 10\n",
    "        rs = np.zeros((size[0], 10))\n",
    "        for i0 in range(0,size[0]):\n",
    "            tmp=rs2[i0]\n",
    "            rs[i0]=np.add(np.dot(f2_w, tmp), f2_b)\n",
    "        # LOG SOFTMAX\n",
    "        rs=rs\n",
    "        for i in range(0, size[0]):\n",
    "            if np.argmax(rs[i])==target[i]:\n",
    "                #print(np.argmax(rs[i]),target[i])\n",
    "                correct+=1\n",
    "            total+=1\n",
    "        t2=time.time()\n",
    "        print ('accuracy=',float(correct)/float(total))\n",
    "        print('Inference Time',t2-t1)\n",
    "\n",
    "\n",
    "Test_number=10  # number of images  for testing procedure\n",
    "test(Test_number)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
